package com.briup.smartlabs.service.impl;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import com.briup.smartlabs.bean.SmartEnvDetail;
import com.briup.smartlabs.bean.ex.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.briup.smartlabs.bean.ex.Device;
import com.briup.smartlabs.mapper.ex.DeviceMapper;
import com.briup.smartlabs.service.SmartDeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SmartDeviceServiceImpl implements SmartDeviceService {
	@Autowired
	private DeviceMapper deviceExMapper;

	@Override
	public PageInfo<Device> findByCondition(int pageNum, int pageSize, String typeId, String key) {
//1.使用pagehelper开启分页
		PageHelper.startPage(pageNum,pageSize);
//		2.正常条件查询（不用limit）
//		如果要进行模糊查询，在sql中没有加%的话需要在代码中处理
//		if(key !=null) {
//		key = "%"+key+"%";
//		}
		key = key != null? "%"+ key +"%":null;
		List<Device> list =deviceExMapper.findByCondition(typeId,key);

		//4.返回结果
		return new PageInfo<>(list);
	}

	@Override
	public void sendMessage(String command) {
		//获取设备所属实验室的ip和端口
		try(Socket socket = new Socket("localhost",9999);
			PrintStream ps = new PrintStream(
					socket.getOutputStream())){
			ps.println(command);
			ps.flush();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

}





